Skip to content

ignore divide error in streamline #1213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 6, 2018
Merged

ignore divide error in streamline #1213

merged 2 commits into from
Oct 6, 2018

Conversation

Kully
Copy link
Contributor

@Kully Kully commented Oct 3, 2018

Simple fix to warning that pops up when streamline code divides 0 by 0.

See https://plot.ly/python/streamline-plots/#basic-streamline-plot for an example of the warning.

@@ -354,6 +354,7 @@ def get_streamline_arrows(self):
dif_x = arrow_end_x - arrow_start_x
dif_y = arrow_end_y - arrow_start_y

np.seterr(divide='ignore', invalid='ignore')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Kully! I'd like to see if we can also restore the original numpy error state. Could we do something like:

orig_err = np.geterr()
np.seterr(divide='ignore', invalid='ignore')
streamline_ang = np.arctan(dif_y / dif_x)
np.seterr(**orig_err)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that idea. Let's try it.

@Kully
Copy link
Contributor Author

Kully commented Oct 4, 2018

it works as expected - waiting for tests to pass

@jonmmease
Copy link
Contributor

Great! 💃

@jonmmease jonmmease merged commit f9e94c9 into master Oct 6, 2018
@empet
Copy link

empet commented Oct 6, 2018

Hey @jonmmease and @Kully,

In scientific computation it is recommended to use arctan2 instead of arctan to compute angles.
If you need that angle to get the the polar coordinates of the point(x,y), then arctan doesn't give the right
angle (see for example x=-3, y=np.sqrt(3), arctan(y/x)=-0.5235 radians i.e. -30 degrees ). But this point lies in the second quadrant, hence its polar angle cannot be -30 degrees.

On the other hand, arctan2(y,x)=2.6179, i.e. 150 degrees. Hence this is the right polar angle.

arctan has the range (pi/2, pi/2), while arctan2, (-pi, pi]. It is calculated
by the rule illustrated in this figure.

arctan2

Example:

x=np.array([0, 0, 3, -3, -3, 0])
y=np.array([0, 1, np.sqrt(3), np.sqrt(3), -np.sqrt(3), -1])
print(np.arctan(y/x))
print(np.arctan(y/x)*180/np.pi)

[        nan  1.57079633  0.52359878 -0.52359878  0.52359878 -1.57079633]
[ nan  90.  30. -30.  30. -90.]

radians=np.arctan2(y, x)
print(radians)
print(radians*180/np.pi)

[ 0.          1.57079633  0.52359878  2.61799388 -2.61799388 -1.57079633]
[   0.   90.   30.  150. -150.  -90.]

`arctan2` doesn't throw any error, when x=0 or both x=0 and y=0. 

@nicolaskruchten nicolaskruchten deleted the streamline-division branch June 19, 2020 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants